package cn.itrip.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.beans.vo.ItripLabelDicVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.service.image.ImageService;
import cn.itrip.service.labeldic.ItripLabelDicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@Api(value = "API", basePath = "/http://api.itrap.com/api")
@RequestMapping(value = "/api/hotelroom")
@Slf4j
public class HotelRoomController {

    @Resource
    private ImageService imageService;

    @Resource
    private ItripLabelDicService itripLabelDicService;

    @RequestMapping(value = "/getimg/{roomId}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripImageVO> getImg(@PathVariable Integer roomId) {

        List<ItripImageVO> itripImageVOS = new ArrayList<>();
        try {
            if (EmptyUtils.isNotEmpty(roomId)) {
                Map param = new HashMap();
                param.put("type", 1);
                param.put("targetId",roomId);
                List<ItripImage> itripImages = imageService.getImgUrlByMap(param);
                if (EmptyUtils.isNotEmpty(itripImages)) {
                    itripImageVOS = itripImages.stream()
                            .map(e -> new ItripImageVO(e.getPosition(),e.getImgUrl()))
                            .collect(Collectors.toList());
                }
            }else{
                DtoUtil.returnFail("cityId不能为空", "10201");
            }

        } catch (Exception e) {
            DtoUtil.returnFail("系统异常", "10205");
            e.printStackTrace();
        }
        return DtoUtil.returnSuccess("获取酒店房型的图片成功",itripImageVOS);
    }
    @ApiOperation(value = "查询所有床型", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "获取所有床型(用于查询页列表)" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码: </p>" +
            "<p>10205: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhotelroombed", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripLabelDicVO> queryotelroombed() {
        List<ItripLabelDicVO> itripAreaDicVOs = new ArrayList<>();
        try {
            Map param = new HashMap();
            param.put("parentId", 1);
            List<ItripLabelDic> itripLabelDics = itripLabelDicService.getItripLabelDicListByMap(param);
            if (EmptyUtils.isNotEmpty(itripLabelDics)) {
                itripAreaDicVOs = itripLabelDics.stream()
                        .map(e -> new ItripLabelDicVO(e.getId(),e.getName(),e.getDescription(),e.getPic()))
                        .collect(Collectors.toList());
            }
        } catch (Exception e) {
            DtoUtil.returnFail("系统异常", "10205");
            e.printStackTrace();
        }
        return DtoUtil.returnSuccess("获取所有床型成功",itripAreaDicVOs);
    }
}
